function MyString(str) {

    this.str = str;
    // 访问下标
    for (let i = 0; i < this.str.length; i++) {
        this[i] = this.str[i]
    }

    // 长度length
    this.length = function () {
        let count = 0;
        for (var i in this.str) {
            count++;
        }
        return count;
    };

    // toString()
    this.toString = function () {
        let stg = "";
        for (let i in this.str) {
            stg += this.str[i];
        }
        return stg;
    };

    // valueOf();
    this.valueOf = function () {
        return this.toString();
    }

    // charAt
    this.charAt = function (number) {
        number = parseInt(number, 10);
        number = isNaN(number) ? 0 : number;
        return this.str[number];
    }

    // 重写concat
    this.concat = function (opt) {
        return this.str + opt;
    }

    // 重写slice
    this.slice = function (opt1, opt2) {
        opt1 = (isNaN(parseInt(opt1)) ? 0 : parseInt(opt1)) < 0 ? this.str.length + parseInt(opt1) : parseInt(opt1);
        opt2 = (isNaN(parseInt(opt2)) ? 0 : parseInt(opt2)) < 0 ? this.str.length + parseInt(opt2) : parseInt(opt2);

        let temp = "";
        for (let i = opt1; i < opt2; i++) {
            temp += this.str[i];
        }
        return temp;
    }

    // 切割
    this.split = function (opt) {
        let arr = [], temp = "";
        if (opt) {
            for (let i in this.str) {
                if (this.str[i] === opt) {
                    arr.push(temp);
                    temp = "";
                } else {
                    temp += this.str[i];
                }
            }
            arr.push(temp);
        } else {
            for (let i in this.str) {
                temp += this.str[i];
            }
        }
        return arr;
    }

    // 反转
    this.reverse = function () {
        let temp = '';
        for (let i = this.str.length - 1; i >= 0; i--) {
            temp += this.str[i];
        }
        return temp;
    }

    // 数组
    this.toArr = function (num) {
        let arr = [];
        for (let i = 0; i < this.str.length; i++) {
            arr.push(this.str[i])
        }
        return arr[num];
    }
}
//
// let test = new MyString('heyBaby,oo,c,bnb');
//
// console.log(test.length());
// console.log(test.slice(1,2));
// console.log(test.slice(1,-1));
// console.log(test.reverse());
// console.log(test.toArr(4));
// console.log(test[4]);

function MyArray() {
    for (let i in arguments) {
        this[i] = arguments[i];
    }

    this.arr = arguments;

    // 长度
    this.length = function () {
        let count = 0;
        for (let i in this.arr) {
            count++;
        }
        return count;
    }

    // 字符串化
    this.toString = function () {
        let str = '';
        for (let i in this.arr) {
            if (i != this.arr.length - 1) {
                str += this.arr[i] + ',';
            } else {
                str += this.arr[i];
            }
        }
        return str;
    }

    // push
    this.push = function (opt) {
        this.arr += ',' + opt;
    }

    // join
    this.join = function (opt) {
        let temp = '';
        for (let i in this.arr) {
            if (opt) {
                if (i != this.arr.length - 1) {
                    temp += this.arr[i] + opt;
                } else {
                    temp += this.arr[i];
                }
            } else {
                temp = this.arr.toString();
            }
        }
        return temp;
    }

}

// let cc = new MyArray(1,2,"qweqw",'p');
// console.log( 'kk' + cc.length())
// console.log( cc[3]);
//
// console.log( cc.toString());
// console.log( cc.join(' -- '));

function MyProto(name, color) {
    this.name = name;
    this.color = color;
    this.info = function () {
        return 'name: ' + this.name + 'color: ' + this.color;
    }
}

let son = new MyProto('kitty', 'yellow');

MyProto.prototype.toSay = function () {
    return "Hi ！ " + this.name + ', ' + 'The color is ' + this.color;
}

// console.log(son.name);
// console.log(son.toSay());
//
// console.log(son.constructor.prototype === MyProto.prototype);
// console.log(son.constructor === MyProto.prototype.constructor);
// console.log(son.constructor === MyProto.prototype.constructor);


let shape = {
    type: 'TYPE',
    getType: function () {
        return this.type
    }
}

function Triangle(opt1, opt2, opt3) {
    this.a = opt1;
    this.b = opt2;
    this.c = opt3;
}

Triangle.prototype = shape;
Triangle.prototype.getPerimeter = function () {
    return this.a + this.b + this.c;
}

// 重设自身constructor
Triangle.prototype.constructor = Triangle;

let t = new Triangle(1, 2, 3);

// for(let i in t) {
//     if(t.hasOwnProperty(i)) {
//         console.log(t[i])
//     }
// }

// console.log(t.constructor === Triangle);
// console.log(shape.isPrototypeOf(t));
// console.log(t.getPerimeter());
// console.log(t.getType());

// 随机打乱函数
Array.prototype.shuffle = function () {
    let i = this.length,
        temp,
        r;
    while (i) {
        r = Math.floor(Math.random() * i--);
        temp = this[r];
        this[r] = this[i];
        this[i] = temp;

        console.log(i + " -- " + this[i])
    }
    return this;
}

// function KK() {
//     this.name = 'lll';
//     this.age = 12;
//     this.say = function () {
//         return this.name;
//     }
// }
//
// function BB() {
//     this.name='11';
// }
//
// BB.prototype = new KK();
//
// BB.prototype.constructor = BB;
//
// var ak = new BB();
// delete ak.age;
// console.log(ak.age)











